# Multipurpose script for deploying solution
# Arguments <operation> [settingsfile]


# Set global settings----------------------------------------------------------
# General
	$VerifyCommands = $false				#if true then commands will not be executed, only printed
	$SettingsFilepath = ".\settings.ps1"	#default location of local settings-file

# Parts to run
    # Install and configure MOSS
        $CreateIndexDirectory = $false
        $CreateFarm = $false
        $ProvisionCentralAdmin = $false
        $InstallServices = $false
        $SecureFSandReg = $false
        $UpdateDBSettings = $false
        $StartMOSSSearch = $false
        $StartWSSSearch = $false
        $InstallAllConfigFeatures = $false

    # Other MOSS configuration settings
        $ConfigureEmail = $false
        $SetRecycleBinSettings = $false
        $SetMaxUploadFileSize = $false
        $EnableSelfService = $false
        $SetMasterSiteDirectory = $false
        $CopyOWSTIMERConfig = $false
        $AddFarmAdministrators = $false

    # WSP Solutions  
        $InstallGlobalSolutions = $true
        $InstallWebApplicationSolutions = $true
		$UnInstallGlobalSolutions = $true
		$UnInstallWebApplicationSolutions = $true
		$UpgradeGlobalSolutions = $true
		$UpgradeWebApplicationSolutions = $true
		$GlobalSolutions = ("Common.wsp", "Branding.wsp") 
		$WebApplicationsSolutions = ("1.wsp", "2.wsp")

    # Web applications to create    
        $CreateSSPWebApp = $false
        $CreateMySiteWebApp = $false
        $CreateTeamWebApp = $true
        $CreateInfoWebApp = $false
		$DeleteTeamWebApp = $true
		$DeleteInfoWebApp= $false
        
    # Restore content databases
        $RestoreInfoDatabase = $false
        
    # Create Sites and sitecollections
        $CreateSSPSite = $false
        $CreateMySite = $false
        $CreateTeamSiteCollection = $true    
        $CreateTeamSubSites = $true
        $AddMeetingsManagedPath = $true
        $CreateMeetingsSiteCollections = $true
        $CreateCOAStuff = $true
    
	# Special steps
        $DeployGlobalResources = $true
        $CreateProjectMembersGroup = $false
		$ActivateMeridio = $false


# Environment
    $username = $env:username
    $userAndDomainName = [System.Security.Principal.WindowsIdentity]::GetCurrent().name
    $email = "info@mydomain.com"
    # $email = (New-Object System.DirectoryServices.DirectorySearcher ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().GetDirectoryEntry()), ("cn=" + $username),  "mail", "Subtree").FindOne().Properties["mail"][0]
    $computerDomain = $(Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters).Domain
    #$fqdn = ($env:COMPUTERNAME + "." + $computerDomain)
    $fqdn = ($env:COMPUTERNAME)
    $TeamWebAppDir = ""

# Computer Settings
    $EmailFromAddress = "${env:COMPUTERNAME}@mydomain.com"
    $SMTPServer = "smtp.mydomain.com"
    $IndexLocation = "C:\Indexes"
    $AdminPort = "8088"
    
# Site settings    
    $SharePoint_AppPool = "SharePointApp"
    $MySite_URL = ("http://" + $fqdn + ":41182")
    $MySite_Description = "MySite (41182)"
    $MySite_AppPool = "SharedServices1"
    $SSP_URL = ("http://" + $fqdn + ":8089")
    $SSP_Description = "Shared Services (8089)"
    $SSP_Title = "MyDomain Solution SSP"
    $Team_URL = ("http://" + $fqdn + ":11102")
    $Team_Description = "Description of Team site"
    $Info_URL = ("http://" + $fqdn)
    $Info_Description = "Description of Info site"

# Sitecollection arrays
    $TeamMeetingsSites = ("test1","test2","test3")
	$TeamSubSites = ("sub1","sub2","sub3")

# Command
    new-alias "stsadm" ($env:CommonProgramFiles + "\Microsoft Shared\web server extensions\12\BIN\stsadm.exe")
    new-alias "psconfig" ($env:CommonProgramFiles + "\Microsoft Shared\web server extensions\12\BIN\psconfig.exe")
    new-alias "sqlcmd" ($env:ProgramFiles + "\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe")


# Databases
    $LocalDBServer = $env:COMPUTERNAME
    # $CommonDBServer = ""
    # $ConfigDB = "Config"
    # $AdminContentDB = "AdminContent"
    # $WSSSearchDB = "WSSSearch"
    # $MySiteDB = "MySiteContent"
    # $SharedServicesDB = "SharedServices1"
    # $SharedServicesContentDB = "SharedServices1_Content"
    # $SharedServicesSearchDB = "SharedServices1_Search"
    $TeamContentDB = $env:COMPUTERNAME + "_WSS_Content"
    $InfoContentDB = ($env:COMPUTERNAME + "_Info_Content")


# Accounts
    $FarmAdminAccount = $env:COMPUTERNAME + "\Administrator"
    $FarmAdminPassword = ""
    $SspServiceAccount = $env:COMPUTERNAME + "\OSS_SSP"
    $SspServicePassword = ""
    $SspAppPoolAccount = $env:COMPUTERNAME + "\OSS_SSP"
    $SspAppPoolPassword = ""
    $ContentAccount = $env:COMPUTERNAME + "\OSS_Site"
    $ContentPassword = ""
    # $SearchSvcAccount = ""
    # $SearchSvcPassword = ""
    # $CrawlAccount = ""
    # $CrawlPassword = ""
    	

function ExecuteCommand([string]$cmd2Execute, [bool]$checkExitCode=$false)
{
	if ($VerifyCommands) 
	{
		echo $cmd2Execute
	}
	else
	{
		invoke-expression "$cmd2Execute"
		if ( $checkExitCode -and $LastExitCode -ne 0)
		{
			Write-Host -foregroundcolor red ("Command failed: " + $cmd2Execute)
			(Read-Host "Manually fix this error and hit <ENTER> to continue")
		}
	}
	return
}
	
# -----------------------------------------------------------------------------


Clear-Host


# Check arguments, otherwise report argument error-----------------------------
if ($args.count -eq 0) 
{
   Write-Host -foregroundcolor red "No arguments specified. Must specify operation: destroy|build|rebuild|upgrade"
   return $false
}
else
{
	if (!($args[0] -eq "build" -or $args[0] -eq "destroy" -or $args[0] -eq "rebuild" -or $args[0] -eq "upgrade"))
	{
	   Write-Host -foregroundcolor red "Invalid argument for operation:" $args[0]
	   return $false
	}
	if ($args.count -gt 1) 
	{
		if (!(Test-Path $args[1]))
		{
		   Write-Host -foregroundcolor red "Specified settings file does not exists"
		   return $false
		}
		$SettingsFilepath = $args[1]
	}
}
# -----------------------------------------------------------------------------


# Include settings file if found-----------------------------------------------
if ((Test-Path $SettingsFilepath)) 
{
	Write-Host -foregroundcolor yellow "# Retrieving local settings from" $SettingsFilepath
	. $SettingsFilepath
}
else 
{
	Write-Host -foregroundcolor yellow "# Using global settings"
}
# -----------------------------------------------------------------------------


# Write active settings
Write-Host -foregroundcolor yellow "# Active settings:"
Write-Host -foregroundcolor yellow ("# $" + "VerifyCommands = " + $VerifyCommands) 
# -----------------------------------------------------------------------------


# Decide operation--------------------------------------------------------------
if ($args[0] -eq "destroy" -or $args[0] -eq "rebuild")
{
	. .\destroy_farm.ps1
}

if ($args[0] -eq "build" -or $args[0] -eq "rebuild")
{
	. .\build_farm.ps1
}

if ($args[0] -eq "upgrade")
{
	. .\upgrade_farm.ps1
}
# -----------------------------------------------------------------------------


Write-Host -foregroundcolor green "# End of deploy"